// pages/find/find.js
const {quest} = require("../../api/request")
function getRandomColor() {
  const rgb = []
  for (let i = 0; i < 3; ++i) {
    let color = Math.floor(Math.random() * 256).toString(16)
    color = color.length === 1 ? '0' + color : color
    rgb.push(color)
  }
  return '#' + rgb.join('')
}

Page({

  /**
   * 页面的初始数据
   */
  data: {
    percent:100,
    flag:true, 
    banners:[],
    poster:"http://47.104.209.44/base/imgs/1.jpg",
    name:"纤夫的爱",
    author:"班子",
    src:"http://47.104.209.44/mp/Sugar1.mp3",
    vSrc:'http://47.104.209.44/mp/brkw1.mp4',
    danmuList:
    [{
      text: '第 1s 出现的弹幕',
      color: '#ff0000',
      time: 1
    }, {
      text: '第 3s 出现的弹幕',
      color: '#ff00ff',
      time: 3
    }],
    word:666,
    toggle:true,
  },
  // video 正在播放 
  videotimeupdate(){
    console.log('timeupdate')
    if(!wx.getStorageSync("isplay") && this.data.toggle ){
      console.log("网络改变了")
      this.showDanger()
      // 开关
      this.setData({
        toggle:false 
      })
   }
  },
  // 视频开始播放
  videoplay(){
     if(!wx.getStorageSync("isplay")){
        this.showDanger()
     }
  },

  showDanger(){
    this.video.pause()
    wx.showModal({
      title: '流量提示',
      content: '你正在使用流量观看视频,请确认是否继续?',
      cancelText:"取消观看",
      cancelColor:"#999",
      confirmText:"继续观看",
      confirmColor:"#f50",
      success: (res)=> {
        if (res.confirm) {
          console.log('用户点击确定')
          this.video.play()
          wx.setStorageSync("isplay",true)  // 直接无视条件直接播放 
          wx.showToast({
            title:"土豪,你请继续",
            icon:'none'
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
          wx.showToast({
            title:"大哥,请充值",
            icon:'none'
          })
        }
      }
    })
  },
  changeVideoSrc(){
    const that = this
    wx.chooseVideo({
      sourceType: ['album', 'camera'],
      maxDuration: 60,
      camera: ['front', 'back'],
      success:(res)=> {
        this.setData({
          vSrc: res.tempFilePath
        })
      }
    })
  },
  sendDanmu(){
    this.video.sendDanmu({
      text: this.data.word,
      color: getRandomColor()
    })
    this.setData({
      word:''
    })
  },
  audioPlay(){
    this.audioCtx.play()
  },
  audioPause(){
    this.audioCtx.pause()
  },
  async getYunBanner(){
    let res = await quest('http://47.104.209.44:3333/banner','get')
    console.log(res)
    this.setData({
      banners:res.data.banners.slice(0,4)
    })
  },

  timeend(){
   this.setData({
     flag:false 
   })
   this.getYunBanner()
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.audioCtx = wx.createAudioContext('myAudio')
    this.video= wx.createVideoContext('myVideo')

    // 获取网络状态 
    wx.getNetworkType({
      success: (res)=> {
        const networkType = res.networkType
        console.log(networkType)
        wx.setStorageSync("isplay",networkType=='wifi')  // true 可以播放  
      }
    })

    // 监听网络状态改变  
    wx.onNetworkStatusChange(function (res) {
      console.log(res.isConnected)
      console.log(res.networkType)
      wx.setStorageSync("isplay",res.networkType=='wifi')  // true 可以播放  
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})